var express = require('express');
var router = express.Router();
const { uploadFile } = require('../untile/upload.js')

const { addGoods, getGoods, deleteGoodsById, updateGoodsById, searchGoods, getGoodsByPage } = require('../server/goodsServer.js')

//获取商品
router.get('/', async (req, res, next) => {
  const data = await getGoods(req.query)
  res.send(data)
});
//新增商品
router.post('/addGoods', async (req, res, next) => {
  const data = await addGoods(req.body)
  res.send(data)
});
//删除商品
router.post('/deleteGoodsById', async (req, res) => {
  res.send(await deleteGoodsById(req.body));
});
//修改商品信息
router.post('/updateGoodsById', async (req, res) => {
  res.send(await updateGoodsById(req.body));
});
//图片上传到后台
router.post('/addImages', async (req, res) => {
  const data = await uploadFile(req, res, {
    fileType: 'images',
    path: './public'
  });
  res.send(data);
});
//查询商品
router.post('/searchGoods', async (req, res) => {
  res.send(await searchGoods(req.body));
});
//分页获取
router.get('/getGoodsByPage', async (req, res) => {
  res.send(await getGoodsByPage(req.query));
});

module.exports = router;
